package com.mzp.goods.goods.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.mzp.goods.goods.dao.GoodsDao;
import com.mzp.goods.goods.pojo.Goods;

/**
 * @Description goods 服务层
 * @author admin
 * @date 2021-04-17 15:46:11
*/
@Service
public class GoodsService {

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<Goods> findAll() {
		return goodsDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<Goods> findSearch(Map whereMap, int page, int size) {
		Specification<Goods> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return goodsDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<Goods> findSearch(Map whereMap) {
		Specification<Goods> specification = createSpecification(whereMap);
		return goodsDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public Goods findById(String id) {
		return goodsDao.findById(id).get();
	}

	/**
	* 增加
	* @param goods
	*/
	public void add(Goods goods) {
		goods.setId( idWorker.nextId()+"" );
		goodsDao.save(goods);
	}

	/**
	* 修改
	* @param goods
	*/
	public void update(Goods goods) {
		goodsDao.save(goods);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		goodsDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<Goods> createSpecification(Map searchMap) {

		return new Specification<Goods>() {

			@Override
			public Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//title
				if (searchMap.get("title")!=null && !"".equals(searchMap.get("title"))) {
					predicateList.add(cb.like(root.get("title").as(String.class), "%"+(String)searchMap.get("title")+"%"));
				}
				//desc
				if (searchMap.get("desc")!=null && !"".equals(searchMap.get("desc"))) {
					predicateList.add(cb.like(root.get("desc").as(String.class), "%"+(String)searchMap.get("desc")+"%"));
				}
				//price
				if (searchMap.get("price")!=null && !"".equals(searchMap.get("price"))) {
					predicateList.add(cb.like(root.get("price").as(String.class), "%"+(String)searchMap.get("price")+"%"));
				}
				//mainimage
				if (searchMap.get("mainimage")!=null && !"".equals(searchMap.get("mainimage"))) {
					predicateList.add(cb.like(root.get("mainimage").as(String.class), "%"+(String)searchMap.get("mainimage")+"%"));
				}
				//publishdate
				if (searchMap.get("publishdate")!=null && !"".equals(searchMap.get("publishdate"))) {
					predicateList.add(cb.like(root.get("publishdate").as(String.class), "%"+(String)searchMap.get("publishdate")+"%"));
				}
				//status
				if (searchMap.get("status")!=null && !"".equals(searchMap.get("status"))) {
					predicateList.add(cb.like(root.get("status").as(String.class), "%"+(String)searchMap.get("status")+"%"));
				}
				//repertory
				if (searchMap.get("repertory")!=null && !"".equals(searchMap.get("repertory"))) {
					predicateList.add(cb.like(root.get("repertory").as(String.class), "%"+(String)searchMap.get("repertory")+"%"));
				}
				//visit
				if (searchMap.get("visit")!=null && !"".equals(searchMap.get("visit"))) {
					predicateList.add(cb.like(root.get("visit").as(String.class), "%"+(String)searchMap.get("visit")+"%"));
				}
				//is_del
				if (searchMap.get("isDel")!=null && !"".equals(searchMap.get("isDel"))) {
					predicateList.add(cb.like(root.get("isDel").as(String.class), "%"+(String)searchMap.get("isDel")+"%"));
				}
				//groupid
				if (searchMap.get("groupid")!=null && !"".equals(searchMap.get("groupid"))) {
					predicateList.add(cb.like(root.get("groupid").as(String.class), "%"+(String)searchMap.get("groupid")+"%"));
				}
				//imagesid
				if (searchMap.get("imagesid")!=null && !"".equals(searchMap.get("imagesid"))) {
					predicateList.add(cb.like(root.get("imagesid").as(String.class), "%"+(String)searchMap.get("imagesid")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
